package com.usian.listener;

import com.usian.service.SearchItemService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author chen
 * @date 2021/1/19 - 23:35 - 星期二
 * @project usian_parent
 * @package com.usian.listener
 * @by GGbomd猪猪侠
 */
@Component
public class SearchMQListener {

    @Autowired
    private SearchItemService searchItemService;

    /**
     * 监听者接收消息三要素：
     *  1、queue
     *  2、exchange
     *  3、routing key
     *  声明这个方法是一个消费者方法
     */
    @RabbitListener(bindings = @QueueBinding( //指定绑定关系
            value = @Queue(value="search_queue",durable = "true"), // search_queue 创建一个queue队列  durable = "true"代表消息队列持久化 默认是不持久化状态关闭的false
            exchange = @Exchange(value="item_exchage",type= ExchangeTypes.TOPIC), //Exchange value="item_exchage"所绑定的交换机名必须和前面定义的一样   type= ExchangeTypes  是什么类型消息模型
            key= {"item.*"} //队列和交换机绑定的RoutingKey 这里的RoutingKey值必须和前面定义的一样 *匹配一个词   #可以匹配多个词
    ))
    public void listen(String msg) throws Exception {
        System.out.println("接收到消息：" + msg);
        int result = searchItemService.insertDocument(msg);
        if(result>0){
            throw new RuntimeException("同步失败");
        }
    }
}
